package com.ssm.util.test.xml;

import java.io.FileOutputStream;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;

import com.ssm.util.test.PathTest;

public class PraseXML {
	
	//dom4j-1.6.1.jar解析xml文档
	@Test
	public void test1() throws Exception{
		 URL url = PathTest.class.getResource("/");
		 int indexOf = url.toString().indexOf("test-classes");
		 String filepath = url.toString().substring(6,indexOf)+"test-classes/spring-mybatis.xml";
		 SAXReader reader = new SAXReader();
	     Document document = reader.read(filepath);//读取xml文档得到文档的根节点：
//	     System.out.println("文件名："+document.getName());
//	     System.out.println("文件路径："+document.getPath());
//	     System.out.println("文件编码："+document.getXMLEncoding());
	     Element root = document.getRootElement();//得到xml文档根节点元素
	     System.out.println("根节点名："+root.getName());
	     System.out.println("根节点路径："+root.getPath());
	     
	     List<Attribute> attrList = root.attributes();	//获取节点属性值
	     for (Attribute attribute : attrList) {
	    	System.out.println(attribute.getName() + "=" + attribute.getValue());
	     }
	     
	     Iterator<Element> beans = root.elementIterator("beans");//获取子节点
	     while (beans.hasNext()) {
	    	 Element element2 = beans.next();
	    	 Iterator<Element> iter = element2.elementIterator("alias");//获取子节点
		     while (iter.hasNext()) {
	             Element recordEle = (Element) iter.next();
	             String description = recordEle.elementTextTrim("description"); // 拿到head节点下的子节点title值
	             System.out.println("description:" + description);
	         }
		     
		     List<Element> elementList = element2.elements();//获取根节点的子节点：
		     Iterator<Element> it = elementList.iterator();//得到子节点之后就是自由发挥的时候了，比如遍历子节点得到子节点的结点名称和节点的值。
		     while (it.hasNext()){
		    	 Element element = it.next();
		    	 System.out.println("节点的名称：" + element.getName() + " 节点的值：" + element.getText()); 
		     }
	     }
	}
	
	//创建xml
	@Test
	public void test2() throws Exception{
		Document doc = DocumentHelper.createDocument();
//		DocumentHelper.parseText(xml);//方法可以解析xml字符串
        //创建节点操作对象
        Element rootElement = doc.addElement("student");
        //创建根节点对象rootElement,标签名为student
        rootElement.setText("hello");
        //对节点设置内容,后面读取XML文件的时候可以把他取出来
        rootElement.addAttribute("id","030511111");
        //设置节点属性
        Element name = rootElement.addElement("name");
        //添加子节点
        name.setText("xiaozhang");
        //设置子节点内容
        Element age = rootElement.addElement("age");
        age.setText("23");
        
        Element element = rootElement.element("age");
        rootElement.remove(element);
       
        FileOutputStream fmt = new FileOutputStream("D://a.xml");
        OutputFormat outputFormat =new OutputFormat();
        outputFormat.setEncoding("gb2312");//创建输出格式对象
        // outputFormat.setSuppressDeclaration(true); //是否生产xml头
        outputFormat.setIndent(true); // 设置是否缩进
        outputFormat.setIndent("    "); // 以四个空格方式实现缩进
        outputFormat.setNewlines(true); // 设置是否换行
        
        XMLWriter writer = new XMLWriter(outputFormat);
        writer.setOutputStream(fmt);
        writer.write(doc);
        writer.close();
	}
	
	
}
